

package com.mera.tektronix.csviewer.services

import org.apache.commons.logging.LogFactory

import com.mera.tektronix.csviewer.common.api.*
import com.mera.tektronix.csviewer.common.dto.*
import com.mera.tektronix.csviewer.domain.*

class CompanyService {
	static transactional = true
	private static final log = LogFactory.getLog(CompanyService.class)
	
	ICompanyService companyServiceProxy
	
    def getElement(params){
		log.debug(params)
		
		CompanyDto company = null;
		
		try {
			company = getCompany(params);
		} catch(Exception ex) {
			return [
				success      : 'false',
				errorMessage : ex.getLocalizedMessage()
			];
		}
		
		if(company == null) {
			return [
				success      : 'false',
				errorMessage : "No Company has been found."
			]
		}
		
		return [ 
			id        : company.id, 
			name      : company.name, 
			email     : company.email, 
			address   : company.address,
			phone     : company.phone,
			success   : 'true'
		]
	}
	
	CompanyDto getCompany(params) {
		if(params.companyId != null) {
			return companyServiceProxy.getCompanyById(Long.valueOf(params.companyId));
		}

		if(params.companyName != null) {
			return companyServiceProxy.getCompanyByName(params.companyName);
		}
		
		return null;
	}
}
